package com.corn.turtorial.consumer.listener;

import com.corn.turtorial.consumer.mapper.Point;
import com.corn.turtorial.consumer.mapper.PointMapper;
import com.corn.turtorial.consumer.service.PointService;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.annotation.SelectorType;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.apache.rocketmq.spring.support.RocketMQUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 * @author : Jim Wu
 * @version 1.0
 * @function :
 * @since : 2022/5/9 16:18
 */
@Slf4j
@Component
@RocketMQMessageListener(topic = "txMsg", consumerGroup = "pointGroup", selectorExpression = "user", selectorType = SelectorType.TAG)
public class UserMQListener implements RocketMQListener<String> {

    @Autowired
    private ObjectMapper objectMapper;

    @Autowired
    private PointMapper pointMapper;

    @SneakyThrows
    @Override
    public void onMessage(String message) {
        log.info("收到MQ消息:{}", message);
        Map userMap = objectMapper.readValue(message, Map.class);
        Point point = new Point();
        point.setPoint(5);
        point.setUserId(Long.valueOf(userMap.get("id").toString()));
        pointMapper.insert(point);
    }
}
